package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SysCompanyContactConfigConstant;
import com.nineclock.common.constant.SysCompanyUserConstant;
import com.nineclock.common.entity.UserInfo;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.CurrentUserHolder;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author yangchao
 * @create 2022-01-14 14:41
 **/
@Service
@Transactional
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private SysCompanyUserMapper sysCompanyUserMapper;

    /**
     * 查询当前用户企业所有员工的列表
     * @return
     */
    @Override
    public List<SysCompanyUser> selectOrganizationMemberList() {

        //判断用户是否登录
        if(CurrentUserHolder.get() == null){
            throw new NcException(ResponseEnum.UNAUTHORIZED);
        }

        //获取当前用户信息
        UserInfo userInfo = CurrentUserHolder.get();
        //当前用户ID
        Long userId = userInfo.getId();
        //当前所在企业ID
        Long companyId = userInfo.getCompanyId();

        //查询当前企业所有员工的列表（不包含当前用户）
        LambdaQueryWrapper<SysCompanyUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysCompanyUser::getCompanyId,companyId);
        lambdaQueryWrapper.ne(SysCompanyUser::getUserId,userId);
        lambdaQueryWrapper.eq(SysCompanyUser::getEnable, SysCompanyUserConstant.STATUS_ENABLE);

        return sysCompanyUserMapper.selectList(lambdaQueryWrapper);

    }

}
